package com.codedancer.campus.manager.api.lostFoundItem;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.lostFoundItem.ro.LostFoundItemExamineRo;
import com.codedancer.campus.manager.api.lostFoundItem.ro.LostFoundItemPageSearchRo;
import com.codedancer.campus.manager.service.ILostFoundItemService;
import com.codedancer.campus.manager.service.bo.lostFoundItem.LostFoundItemExamineBo;
import com.codedancer.campus.manager.service.bo.lostFoundItem.LostFoundItemPageSearchBo;
import com.codedancer.campus.manager.service.vo.lostFoundItem.LostFoundItemPageVo;
import com.codedancer.campus.manager.service.vo.lostFoundItem.LostFoundItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/lost-found-item")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "失物招领Api", description = "失物招领Api", tags = "失物招领Api")
public class LostFoundItemApi extends BaseApi {

    @Autowired
    private ILostFoundItemService lostFoundItemService;

    @GetMapping("/page")
    @ApiOperation("分页查询物品")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<IPage<LostFoundItemPageVo>> page(@ApiParam(value = "查询Ro", required = false)
        LostFoundItemPageSearchRo lostFoundItemPageSearchRo) {
        LostFoundItemPageSearchBo lostFoundItemPageSearchBo = BeanUtil.copyProperties(
            lostFoundItemPageSearchRo, LostFoundItemPageSearchBo.class);
        return success(lostFoundItemService.page(lostFoundItemPageSearchBo));
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("详情")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<LostFoundItemVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        return success(lostFoundItemService.detail(uuid));
    }

    @PutMapping("/no-pass/{uuid}")
    @ApiOperation("审核未通过")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> noPass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid,
        @RequestBody @ApiParam(value = "物品审核Ro", required = true) LostFoundItemExamineRo lostFoundItemExamineRo) {
        LostFoundItemExamineBo lostFoundItemExamineBo = BeanUtil
            .copyProperties(lostFoundItemExamineRo, LostFoundItemExamineBo.class);
        lostFoundItemExamineBo.setCurrentUserUuid(currentLoginUuid());
        lostFoundItemExamineBo.setUuid(uuid);
        lostFoundItemService.noPass(lostFoundItemExamineBo);
        return success();
    }

    @PutMapping("/pass/{uuid}")
    @ApiOperation("审核通过")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> pass(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid,
        @RequestBody @ApiParam(value = "物品审核Ro", required = true) LostFoundItemExamineRo lostFoundItemExamineRo) {
        LostFoundItemExamineBo lostFoundItemExamineBo = BeanUtil
            .copyProperties(lostFoundItemExamineRo, LostFoundItemExamineBo.class);
        lostFoundItemExamineBo.setCurrentUserUuid(currentLoginUuid());
        lostFoundItemExamineBo.setUuid(uuid);
        lostFoundItemService.pass(lostFoundItemExamineBo);
        return success();
    }

    @PutMapping("/publish/{uuid}")
    @ApiOperation("发布")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> publish(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.publish(uuid);
        return success();
    }

    @PutMapping("/revoke/{uuid}")
    @ApiOperation("撤回")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> revoke(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.revoke(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.delete(uuid);
        return success();
    }

    @PostMapping("/export")
    @ApiOperation("导出物品")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public void export(HttpServletRequest request, HttpServletResponse response,
        @ApiParam(value = "查询Ro", required = false) LostFoundItemPageSearchRo lostFoundItemPageSearchRo) {
        LostFoundItemPageSearchBo lostFoundItemPageSearchBo = BeanUtil
            .copyProperties(lostFoundItemPageSearchRo, LostFoundItemPageSearchBo.class);
        lostFoundItemService.export(request, response, lostFoundItemPageSearchBo);
    }

    @PutMapping("/batch-revoke")
    @ApiOperation("批量撤回")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> batchRevoke(@RequestBody List<String> uuids) {
        lostFoundItemService.batchRevoke(uuids);
        return success();
    }

    @PutMapping("/batch-publish")
    @ApiOperation("批量发布")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> batchPublish(@RequestBody List<String> uuids) {
        lostFoundItemService.batchPublish(uuids);
        return success();
    }

    @DeleteMapping("/batch-delete")
    @ApiOperation("批量删除")
    @InterfacePermission(encodes = {"superAdmin","lostFoundItemAdmin"})
    public ApiResponse<Void> batchDelete(@RequestBody List<String> uuids) {
        lostFoundItemService.batchDelete(uuids);
        return success();
    }
}
